package com.example.health_life.service;

import com.example.health_life.dao.Userview;
import com.example.health_life.dao.UserviewRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserviewServiceImpl implements UserviewService{

    @Autowired
    private UserviewRepository userviewRepository;

    @Override
    public List<Userview> getUserviewList() {
        return userviewRepository.findAll();
    }

    @Override
    public List<Userview> getUserviewByUserId(Integer userId) {
        return userviewRepository.findByUserId(userId);
    }

    @Override
    public Userview getUserviewById(Integer id) {
        return userviewRepository.findById(id).get();
    }

    @Override
    public Integer getUserviewByTypeId(Byte type, Integer typeId) {
        //获取某类型下的某id的浏览信息的条数
        return userviewRepository.countByTypeAndTypeId(type, typeId);
    }

    @Override
    public Integer addUserview(Userview userview) {
        // 设置浏览时间为当前时间
        userview.setViewTime(java.time.Instant.now());
        return userviewRepository.save(userview).getId();
    }
}
